package com.yesjpt.util;

import com.yesjpt.context.Consts;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期的操作类,
 * @author yqg
 *
 */
public class DateUtil {
	
	/**
	 * 缺省的时间格式
	 */
	//private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss" ;
//	
//	/**
//	 * 采用格式yyyy-MM-dd对日期进行格式化
//	 * @param date 格式:yyyy-MM-dd
//	 * @return
//	 * 格式:yyyy-MM-dd的日期字符串表示
//	 */
//	public static String formatDate(Date date){
//		String pattern = "yyyy-MM-dd" ;
//		return formatDate(date , pattern) ;
//	}
	
	/**
	 * 对日期采用指定的格式进行格式化
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date , String pattern){
		if(pattern == null || pattern.length() == 0){
			pattern = Consts.DF_YYYY_MM_DD;
		}
		if(date == null){
			return "";
		}
		SimpleDateFormat df = new SimpleDateFormat(pattern) ;
		return df.format(date) ;
	}
	public static String Date2String(Date date , String pattern){
		return formatDate(date, pattern);
	}
	public static String Date2String(Date date){
		return formatDate(date, null);
	}
	/**
	 * 将字符串解析成日期对象
	 * @param dateStr 
	 * 	日期的字符串表示形式
	 * @param pattern
	 * 	日期的字符串表示形式的格式
	 * @return
	 */
	public static Date parse(String dateStr, String pattern){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try{
			return sdf.parse(dateStr) ;
		}catch (ParseException e) {
			//e.printStackTrace() ;
		}
		return null ;
	}
	//根据身份证号取生日
	public static Date parseBirthdayByCardId(String cardId){
		try {
			String s = cardId.substring(6, 14);
			System.out.println(s);
			return parse(s, Consts.DF_YYYYMMDD);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	@SuppressWarnings("static-access")
	public static Date addDay(Date date, int days){
		Calendar   calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.DATE, days);//把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime();
		return date;
	}

	@SuppressWarnings("static-access")
	public static Date addMinute(Date date, int minutes){
		Calendar   calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(calendar.MINUTE, minutes);//把日期往后增加一分钟.整数往后推,负数往前移动
		date = calendar.getTime();
		return date;
	}
	/**
	 * 计算两个时间间隔的天数,
	 * preDate是两个时间中靠前的时间,
	 * aftDate是两个时间中靠后的时间,
	 * 如果preDate在aftDate之后，则返回的天数为负数,
	 * 不满24小时按照一天的时间计算(忽略了时分秒),
	 * 例如：2010-03-01 23:59:59 和2010-03-02 00:00:00相隔的时间是1天
	 * @param preDate
	 * @param aftDate
	 * @return 
	 */
	public static int countDay2Date(Date preDate, Date aftDate){
		Calendar c1 = Calendar.getInstance() ;
		Calendar c2 = Calendar.getInstance() ;
		c1.setTime(preDate) ;
		c2.setTime(aftDate) ;
		//将时间c1和时间c2的时 分 秒 都去掉
		setTimeToMidnight(c1) ;
		setTimeToMidnight(c2) ;
		//一天的时间的毫秒数 24*60*60*1000
		long minsPerDays = 86400000 ;
		int days = (int)((c2.getTimeInMillis() - c1.getTimeInMillis())/minsPerDays) ;
		return days ;
	}
	
	
	
	/**
	 * 将时间的时分秒设置为00:00:00
	 * @param c
	 */
	private static void setTimeToMidnight(Calendar c){
		c.set(Calendar.HOUR_OF_DAY, 0);
	    c.set(Calendar.MINUTE, 0);
	    c.set(Calendar.SECOND, 0);
	}
	
	/**
	 * 计算给定时间离当前时间的天数,date是定义为当前时间靠前的时间,
	 * 如果date在当前时间之后，则返回的天数为负数, 
	 * 不满24小时按照一天的时间计算(忽略了时分秒), 
	 * 例如：2010-03-01 23:59:59 和2010-03-02 00:00:00相隔的时间是1天
	 * @param date
	 * @return
	 *  离当前时间的天数
	 */
	public static int countDay2Today(Date date){
		return countDay2Date(date, new Date()) ;
	}
	
	/**
	 * 将时间转化为Calendar形式
	 * @param date
	 * @return
	 */
	public static Calendar parseCalendar(Date date){
		Calendar c = Calendar.getInstance() ;
		c.setTime(date) ;
		
		return c ;
	}
	
	/**
	 * 将日历转换为Date形式
	 * @param c
	 * @return
	 */
	public static Date parseDate(Calendar c){
		return c.getTime() ;
	}
	
	/**
	 * 得到一个时间的当天的开始时间 
	 * 例如:2010-10-8 11:04:05则返回时间设置2010-10-8 00:00:00
	 * @param date
	 * @return
	 */
	public static Date getTheDateOfStartTime(Date date){
		Calendar c = parseCalendar(date);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		return c.getTime() ;
	}
	
	/**
	 * 得到一个时间的当天的最后时间 
	 * 例如:2010-10-8 11:04:05则返回时间设置2010-10-8 23:59:59
	 * @param date
	 * @return
	 */
	public static Date getTheDateOfEndTime(Date date){
		Calendar c = parseCalendar(date);
		c.set(Calendar.HOUR_OF_DAY, 23);
		c.set(Calendar.MINUTE, 59);
		c.set(Calendar.SECOND, 59);
		return c.getTime() ;
	}
	/***
	 * 取得当前月份的开始和结束时间
	 * @return
	 */
	public static Map<String, Date> findCurrentMonthStartDateAndEndDate(){
    	Calendar cal=Calendar.getInstance();
    	Calendar cal2=Calendar.getInstance();
    	cal.set(Calendar.DAY_OF_MONTH, 1);
    	//cal.set(cal.,cal.DAY_OF_MONTH,1);
    	Date startDate=cal.getTime();
    	int year = cal.get(Calendar.YEAR);
        int humanMonth = cal.get(Calendar.MONTH) + 1;
        boolean isRuiNian = (year % 4 == 0);
        int lastDate=0;
        switch (humanMonth)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                lastDate=31;
            case 2:
                 if(isRuiNian)
                 { lastDate=29;}else{ lastDate=28;};
            default:
                lastDate=30;
        }
        
       // cal2.set(cal2.YEAR,cal2.DAY_OF_MONTH,lastDate);
        cal2.set(Calendar.DAY_OF_MONTH,lastDate);
        Date endDate=cal2.getTime();
        
        
        Map<String, Date> map=new HashMap<String, Date>();
        map.put("startDate", startDate);
        map.put("endDate",endDate);
        return map;
        
        
    }
	
	
	public static String countOverTime(Date limitTime){
		long now = DateUtil.parse("2014-01-15 17:29:12", "yyyy-MM-dd HH:mm:ss").getTime() ;
		long overTime = now - limitTime.getTime() ;
		System.out.println(overTime);
		if(overTime > 30*24*60*60*1000*1000*1000*1000){
			return "一月以上" ;
		}else if(overTime > 15*24*60*60*1000*1000*1000*1000){
			return "15天以上" ;
		}else if(overTime > 7*24*60*60*1000*1000*1000*1000){
			return "一周以上" ;
		}else if(overTime > 3*24*60*60*1000*1000*1000*1000){
			return "3天以上" ;
		}else if(overTime > 2*24*60*60*1000*1000*1000*1000){
			return "2天以上" ;
		}else if(overTime > 1*24*60*60*1000*1000*1000*1000){
			return "24小时以上" ;
		}else if(overTime > 12*60*60*1000*1000*1000*1000){
			return "12小时以上" ;
		}else if(overTime > 6*60*60*1000*1000*1000*1000){
			return "6小时以上" ;
		}else if(overTime > 2*60*60*1000*1000*1000*1000){
			return "2小时以上" ;
		}else if(overTime > 1*60*60*1000*1000*1000*1000){
			return "1小时以上" ;
		}else if(overTime > 30*60*1000*1000*1000){
			return "30分钟以上" ;
		}else if(overTime <= 30*60*1000*1000*1000 && overTime>0){
			return "30分钟内" ;
		}
		return "未超时" ;
	}
	//查询数据库服务器上的当前时间年月日时分秒
	public static String funYMDHMSNow() {
		return formatDate(new Date(), Consts.DF_YYYY_MM_DD_HH_MM_SS);
	}
	public static String funYYYYMMDDNow() {
		return formatDate(new Date(), Consts.DF_YYYY_MM_DD);
	}
	
	public static int getAge(Date dateOfBirth) {
		if(dateOfBirth == null) return -1;
		int age = 0;
		Calendar born = Calendar.getInstance();
		Calendar now = Calendar.getInstance();
		if(dateOfBirth != null) {
			now.setTime(new Date());
			born.setTime(dateOfBirth);
			if(born.after(now)) {
				return -1;
			}
			age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
			if (now.get(Calendar.DAY_OF_YEAR) < born.get(Calendar.DAY_OF_YEAR)) {age -= 1;}
		}
		return age;
	}
	//计算年龄
	public static Long age(Date mydate){
		java.util.Date date=new Date();     
		//java.util.Date mydate= parse("2002-01-03", "yyyy-MM-dd");
		long day=(date.getTime()-mydate.getTime())/(24*60*60*1000) + 1;
		String year = new java.text.DecimalFormat("#.00").format(day/365f);
		Double d = Double.parseDouble(year);
		Long age = Math.round(d);
		//System.out.println("niu:" + year);
		return age;
	}
	public static void main(String[] args){
		System.out.println(funYMDHMSNow());
		/*
		Date td = DateUtil.parse("2016-11-15 17:29:11", "yyyy-MM-dd HH:mm:ss");
		//age();
		System.out.println(countOverTime(td)) ;
		System.out.println(getAge(DateUtil.parse("2012-12-09", "yyyy-MM-dd")));
		System.out.println(parseBirthdayByCardId("510103197008017373"));
		*/
	}
	//判断当前时间为星期几，颜治平作品，必为精品
	public static String dayForWeek(Date date){
		Calendar c = Calendar.getInstance() ;
		c.setTime(date) ;
		int dayForWeek=c.get(Calendar.DAY_OF_WEEK)-1;
		String weeks[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
		return weeks[dayForWeek];  
	}
	
	
}
